/**
 * FileName: Exercise1717.c
 * --------------------------------------------------------------------------------------------------------------------
 * 17.17 Give the adjacency-matrix representations of the three graphs depicted in Figure 17.2.
 *
 */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

//边相关
typedef struct {
    int v;
    int w;
}
Edge;

//图相关
typedef struct graph *Graph;
struct graph {
    int V;
    int E;
    int** adj;
};

//辅助函数声明
int** MATRIXinit(int, int, int);
Edge EDGE(int, int);
void GRAPHshow(Graph);

//图操作声明
Graph GRAPHinit(int);
void GRAPHinsertE(Graph, Edge);
void GRAPHremoveE(Graph, Edge);
int GRAPHedges(Edge [], Graph);
Graph GRAPHcopy(Graph);
void GRAPHdestroy(Graph);

//辅助函数实现
/**
 * Program 17.4 Adjacency-matrix allocation and initialization
 * -------------------------------------------------------------------------------------------------------------
 * This program uses the standard C array-of-arrays representation for the two-dimensional adjacency matrix (see Section 3.7).
 * It allocates `r` rows with `c` integers each, then initializes all entries to the value `val`.
 *
 * The call `MATRIXinit(V, V, 0)` in Program 17.3 takes time proportional to $V^2$ to create a matrix that
 * represents a V-vertex graph with no edges.
 *
 * For small $V$, the cost of $V$ calls to `malloc` might predominate.
 */
int** MATRIXinit(int r, int c, int val) {
    int i;
    int j;
    int** t = malloc(r * sizeof(int*));
    for (i = 0; i < r; i++) {
        t[i] = malloc(c * sizeof(int));
    }
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            t[i][j] = val;
        }
    }
    return t;
}

Edge EDGE(int v, int w) {
    Edge edge;
    edge.v = v;
    edge.w = w;
    return edge;
}

void GRAPHshow(Graph G) {
    int i;
    int j;
    printf("%d vertices, %d edges\n", G->V, G->E);
    //邻接矩阵
    // for (i = 0; i < G->V; i++) {
    //     printf("%2d:", i);
    //     for (j = 0; j < G->E; j++) {
    //         printf(" %2d", G->adj[i][j]);
    //     }
    //     printf("\n");
    // }

    //邻接列表
    for (i = 0; i < G->V; i++) {
        printf("%2d:", i);
        for (j = 0; j < G->E; j++) {
            if (G->adj[i][j] == 1) {
                printf(" %2d", j);
            }
        }
        printf("\n");
    }
}

//图操作函数实现
Graph GRAPHinit(int V) {
    Graph G = malloc(sizeof(*G));
    G->V = V;
    G->E = 0;
    G->adj = MATRIXinit(V, V, 0);
    return G;
}

void GRAPHinsertE(Graph G, Edge e) {
    int v = e.v;
    int w = e.w;
    if (G->adj[v][w] == 0) {
        G->E++;
    }
    G->adj[v][w] = 1;
    G->adj[w][v] = 1;
}

void GRAPHremoveE(Graph G, Edge e) {
    int v = e.v;
    int w = e.w;
    if (G->adj[v][w] == 1) {
        G->E--;
    }
    G->adj[v][w] = 0;
    G->adj[w][v] = 0;
}
int GRAPHedges(Edge a[], Graph G) {
    int v;
    int w;
    int E = 0;
    for (v = 0; v < G->V; v++) {
        for (w = v+1; w < G->V; w++) {
            if (G->adj[v][w] == 1) {
                a[E++] = EDGE(v, w);
            }
        }
    }
    return E;
}
Graph GRAPHcopy(Graph);
void GRAPHdestroy(Graph);

//测试函数声明
void test_show();
void test_show2();
void test_show3();

int main(int argc, char *argv[]) {
    test_show();
    test_show2();
    test_show3();
    return 0;
}

//测试函数实现

void test_show() {
    printf("-------The first graph-------------\n");
    int V = 13;
    Graph G = GRAPHinit(V);
    Edge edge1 = EDGE(0, 5);
    Edge edge2 = EDGE(0, 1);
    Edge edge3 = EDGE(0, 2);
    Edge edge4 = EDGE(0, 6);
    Edge edge5 = EDGE(5, 3);
    Edge edge6 = EDGE(5, 4);
    Edge edge7 = EDGE(3, 4);
    Edge edge8 = EDGE(6, 4);
    Edge edge9 = EDGE(7, 8);
    Edge edge10 = EDGE(9, 12);
    Edge edge11 = EDGE(9, 10);
    Edge edge12 = EDGE(9, 11);
    Edge edge13 = EDGE(11, 12);

    GRAPHinsertE(G, edge1);
    GRAPHinsertE(G, edge2);
    GRAPHinsertE(G, edge3);
    GRAPHinsertE(G, edge4);
    GRAPHinsertE(G, edge5);
    GRAPHinsertE(G, edge6);
    GRAPHinsertE(G, edge7);
    GRAPHinsertE(G, edge8);
    GRAPHinsertE(G, edge9);
    GRAPHinsertE(G, edge10);
    GRAPHinsertE(G, edge11);
    GRAPHinsertE(G, edge12);
    GRAPHinsertE(G, edge13);

    GRAPHshow(G);
}

void test_show2() {
    printf("-------The second graph-------------\n");
    int V = 13;
    Graph G = GRAPHinit(V);
    Edge edge1 = EDGE(10, 7);
    Edge edge2 = EDGE(10, 2);
    Edge edge3 = EDGE(10, 6);
    Edge edge4 = EDGE(7, 2);
    Edge edge5 = EDGE(6, 12);
    Edge edge6 = EDGE(2, 12);
    Edge edge7 = EDGE(3, 12);
    Edge edge8 = EDGE(5, 12);
    Edge edge9 = EDGE(1, 8);
    Edge edge10 = EDGE(9, 0);
    Edge edge11 = EDGE(9, 11);
    Edge edge12 = EDGE(0, 11);
    Edge edge13 = EDGE(11, 4);

    GRAPHinsertE(G, edge1);
    GRAPHinsertE(G, edge2);
    GRAPHinsertE(G, edge3);
    GRAPHinsertE(G, edge4);
    GRAPHinsertE(G, edge5);
    GRAPHinsertE(G, edge6);
    GRAPHinsertE(G, edge7);
    GRAPHinsertE(G, edge8);
    GRAPHinsertE(G, edge9);
    GRAPHinsertE(G, edge10);
    GRAPHinsertE(G, edge11);
    GRAPHinsertE(G, edge12);
    GRAPHinsertE(G, edge13);

    GRAPHshow(G);
}

void test_show3() {
    printf("-------The third graph-------------\n");
    int V = 13;
    Graph G = GRAPHinit(V);
    Edge edge1 = EDGE(0, 1);
    Edge edge2 = EDGE(0, 2);
    Edge edge3 = EDGE(0, 6);
    Edge edge4 = EDGE(2, 4);
    Edge edge5 = EDGE(6, 4);
    Edge edge6 = EDGE(4, 3);
    Edge edge7 = EDGE(4, 5);
    Edge edge8 = EDGE(5, 3);
    Edge edge9 = EDGE(7, 8);
    Edge edge10 = EDGE(9, 12);
    Edge edge11 = EDGE(9, 10);
    Edge edge12 = EDGE(9, 11);
    Edge edge13 = EDGE(11, 12);

    GRAPHinsertE(G, edge1);
    GRAPHinsertE(G, edge2);
    GRAPHinsertE(G, edge3);
    GRAPHinsertE(G, edge4);
    GRAPHinsertE(G, edge5);
    GRAPHinsertE(G, edge6);
    GRAPHinsertE(G, edge7);
    GRAPHinsertE(G, edge8);
    GRAPHinsertE(G, edge9);
    GRAPHinsertE(G, edge10);
    GRAPHinsertE(G, edge11);
    GRAPHinsertE(G, edge12);
    GRAPHinsertE(G, edge13);

    GRAPHshow(G);
}
